module.exports = {
    config: {
        asRouteService: true,
        routeRoot: 'portal',
        dependencies: [
        ],
    },
    data: {
        test_keys: {
            User: { type: 'String', refer: 'account' },
            Tool: { type: 'String', refer: 'tools' },
            Key: { type: 'String', required: true, index: true, unique: true },

            __Indexes: [
                {
                    def: {
                        User: 1,
                        Tool: 1,
                    },
                    set: {
                        unique: true,
                    },
                }
            ],
        },
        tests: {
            User: { type: 'String', refer: 'account' },
            Tool: { type: 'String', refer: 'tools' },
            Name: { type: 'String', },
            Description: { type: 'String', },
            tid: { type: 'String' },

            Status: { type: 'String' },
            Result: { type: 'String' },
            ResultPath: { type: 'String' },

            noDetails: { type: 'Boolean', default: false },
        },
    },
    hooks: {
        onModulesReady: async () => {
        },
        onLoadRouters: async () => {
        },
        onRoutersReady: async (app) => {
            app.post('/api/register', async (req, res, next) => {
                res.updateCURD('account', 'C', (op) => {
                    req.body.Saved = true;
                    req.body.Enabled = true;
                    req.body.Deleted = false;
                    req.body.Profile = req.body.Profile || {};
                    const Profile = {
                        Company: req.body.Profile.Company,
                        Industry: req.body.Profile.Industry,
                    };
                    if (op.ctx.body.PhoneNumber) {
                        op.ctx.body.Profile = Profile;
                    }
                })

                return next();
            });

            app.post('/api/login', (req, res, next) => {
                if (req.user && req.user.Profile) {
                    res.addData({
                        id: req.user && req.user.id,
                        Profile: {
                            Company: req.user.Profile.Company || '',
                            Industry: req.user.Profile.Industry || '',
                        },
                    }, false);
                }
                return next();
            });

            app.get('/api/sapplication/list', app.FindDocuments('', false, async (req, res) => {
                const docs = res.locals.data.docs || [];

                for (let i = 0; i < docs.length; i += 1) {
                    const doc = docs[i];
                    
                    const service = await res.app.models.services.findOne({id: doc.服务}, {标题: 1});
                    if (service && service.标题) {
                        doc.服务 = service.标题;
                    }
                }
            }));

            app.get('/api/sapplication/:id/2/view', app.FindDocuments('', false, async (req, res) => {
                const doc = res.locals.data || {};

                const service = await res.app.models.services.findOne({id: doc.服务}, {标题: 1});
                if (service && service.标题) {
                    doc.服务 = service.标题;
                }
            }));


            // for tool result callback, but tools from industryeval!!
            app.post('/api/tools/result/callback',
                async (req, res, next) => {
                    if (!req.body.access_token || !req.body.status || !req.body.result) {
                        res.makeError(400, '参数错误: access_token, status, result');
                        return next('route');
                    }

                    const theKeyMap = await app.models.test_keys.findOne({
                        Key: req.body.access_token,
                    });

                    if (!theKeyMap) {
                        res.makeError(400, '请从工具页面下载工具后进行测试');
                        return next('route');
                    }

                    const theRes = await app.models.tests.create({
                        User: theKeyMap.User,
                        Tool: theKeyMap.Tool,
                        Name: req.body.name || undefined,
                        Description: req.body.description || undefined,
                        tid: req.body.tid || undefined,
                        Status: req.body.status,

                        // TODO: save result to file system??
                        Result: req.body.result,

                        noDetails: true,
                    });

                    // 更新回调次数
                    await res.app.models['industryeval'].updateOne({
                        id: theKeyMap.Tool,
                    }, {
                        $inc: { callbacks: 1 },
                    });
                    
                    res.addData({
                        code: '000',
                        message: 'success',
                        result: {
                            id: theRes.id,
                        },
                    }, true);

                    return next();
                },
            );

        }
    },
    i18n: {
        'en-us': {
            'module-title': 'Portal',
            'module-description': '',

            未发布: 'Not published',
            即将实施: 'Coming soon',
            现行: 'Active',
            废止: 'Abolition',

            taskStatusRUNNING: 'Running',
            taskStatusPENDING: 'Pending',
            taskStatusFINISHED: 'Finished',
            taskStatusCANCELED: 'Canceled',
            taskStatusFAILED: 'Failed',
            taskStatusR: 'Running',
            taskStatusW: 'Pending',
            taskStatusF: 'Finished',
            taskStatusC: 'Canceled',
        },
        'zh-cn': {
            'module-title': '网站主站',
            'module-description': '',
            
            未发布: '未发布',
            即将实施: '即将实施',
            现行: '现行',
            废止: '废止',

            taskStatusRUNNING: '正在运行',
            taskStatusPENDING: '挂起',
            taskStatusFINISHED: '完成',
            taskStatusCANCELED: '取消',
            taskStatusFAILED: '失败',
            taskStatusR: '正在运行',
            taskStatusW: '挂起',
            taskStatusF: '完成',
            taskStatusC: '取消',
        },
    },
}